package com.heima.behavior.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.behavior.dto.ArticleInfoBehaviorDto;
import com.heima.behavior.dto.ArticleInfoDtoV2;
import com.heima.behavior.dto.EntryDto;
import com.heima.behavior.entity.ApBehaviorEntry;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.heima.behavior.service.IApUnlikesBehaviorServiceV2;
import com.heima.common.dto.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.behavior.service.IApBehaviorEntryService;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PreDestroy;

/**
 * <p>
 * APP行为实体表,一个行为实体可能是用户或者设备，或者其它 前端控制器
 * </p>
 *
 * @author mcm
 * @since 2022-03-17
 */
@RestController
@RequestMapping("/api/v1/behavior_entry")
@Api(tags = "APP行为实体表,一个行为实体可能是用户或者设备，或者其它接口")
public class ApBehaviorEntryController{

    @Autowired
    private IApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private IApFollowBehaviorService apFollowBehaviorService;
    @Autowired
    private IApUnlikesBehaviorServiceV2 apUnlikesBehaviorServiceV2;
    @Autowired
    private IApBehaviorEntryService entryService;
    @Autowired
    private IApLikesBehaviorService apLikesBehaviorService;

    private ExecutorService executorService= Executors.newCachedThreadPool();

    @PreDestroy
    public void shutdown()
    {
        this.executorService.shutdown();
    }

    /**
     * 根据entry_id查询entry
     * @param dto
     * @return
     */
    @PostMapping("/getEntry")
    public ResponseResult<ApBehaviorEntry> getEntry(@RequestBody EntryDto dto){
        // 调用service
        return ResponseResult.okResult(apBehaviorEntryService.getEntry(dto));
    }
    @PostMapping("/handle/behavior")
    /**
     * 远程调用，获取userId对应的entryId，
     * 然后把一些行为参数返回
     * 这里我用异步缩短响应
     */
    public ResponseResult handleBehavior(@RequestBody ArticleInfoDtoV2 dto) throws ExecutionException, InterruptedException {
        EntryDto entryDto = new EntryDto();
        BeanUtils.copyProperties(dto,entryDto);
        ArticleInfoBehaviorDto articleInfoBehaviorDto = new ArticleInfoBehaviorDto();
        Integer entryId = entryService.getEntry(entryDto).getId();
        articleInfoBehaviorDto.setEntryId(entryId);
        dto.setEntryId(entryId);
        Future<Boolean> islike = executorService.submit(() -> {
            return apLikesBehaviorService.isLike(dto);
        });
        Future<Boolean> isFollow = executorService.submit(() -> {
            return apFollowBehaviorService.isFollow(dto);
        });
        Future<Boolean> isUnlike = executorService.submit(() -> {
            return apUnlikesBehaviorServiceV2.isUnlike(dto);
        });
       /* articleInfoBehaviorDto.setIslike(apLikesBehaviorService.isLike(dto));
        articleInfoBehaviorDto.setIsfollow(apFollowBehaviorService.isFollow(dto));
        articleInfoBehaviorDto.setIsunlike(apUnlikesBehaviorServiceV2.isUnlike(dto));*/
    articleInfoBehaviorDto.setIslike(islike.get());
    articleInfoBehaviorDto.setIsfollow(isFollow.get());
    articleInfoBehaviorDto.setIsunlike(isUnlike.get());
        return ResponseResult.okResult(articleInfoBehaviorDto);
    }


}
